package com.hyzx.qbasic.admin.controller;

import com.hyzx.qbasic.admin.domain.service.IPermissionService;
import com.hyzx.qbasic.admin.common.security.ShiroUtils;
import com.hyzx.qbasic.admin.model.annotation.DbLogIgnore;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.dto.AdminUserInfo;
import com.hyzx.qbasic.admin.model.dto.MenuListInfo;
import com.hyzx.qbasic.admin.model.vo.req.admin.ChangePassReq;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.domain.service.IAdminUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 黄江华 on 2017/9/20
 */

@ApiIgnore
@Controller
public class HomeController extends BaseController {
    @Autowired
    private IAdminUserService userService;

    @Autowired
    private IPermissionService permissionService;

    @SysLog("主页框架页")
    @GetMapping({"/", "index"})
    public String index(Model model) {
        AdminUserInfo user = ShiroUtils.getCurrentUserInfo();
        String userName = !StringUtils.isEmpty(user.getRealName()) ?
                user.getRealName() : user.getUserName();
        model.addAttribute("userName", userName);

        //获取用户菜单列表
        int userId = user.getUserId();
        List<MenuListInfo> menuList = permissionService.getUserMenuList(userId);
        model.addAttribute("menuList", menuList);

        return "index";
    }

    @SysLog("修改密码页")
    @GetMapping("/changePass")
    public String changePass() {
        return "system/user/changePass";
    }

    @SysLog("修改密码表单提交")
    @PostMapping("/changePass")
    @ResponseBody
    public BaseResModel<BaseOperationRes> changePass(@Valid @RequestBody ChangePassReq req, BindingResult result) {
        AdminUserInfo user = ShiroUtils.getCurrentUserInfo();
        req.setUserId(user.getUserId());
        req.setUserName(user.getUserName());

        BaseOperationRes res = userService.changePass(req);
        return BaseResUtils.success(res);
    }

    @DbLogIgnore
    @SysLog("框架说明文档")
    @GetMapping("/readme")
    public String readme() {
        return "readme";
    }

    @DbLogIgnore
    @SysLog("QBasic框架接口文档")
    @GetMapping("/docs")
    public String docs() {
        return "redirect:/swagger-ui.html";
    }
}
